package com.itheima.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.domain.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;


@RestController
@Slf4j
@RequestMapping("/shoppingCart")
@Api(tags = "购物车相关接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @param request
     * @return
     */
    @ApiOperation(value = "添加购物车")
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        //获取当前用户信息
        log.info("购物车数据:{}", shoppingCart);
        Long user = (Long) request.getSession().getAttribute("user");
        Long currentId = BaseContext.getCurrentId();
        log.info("当前登录用户id为:{}----{}", user, currentId);
        shoppingCart.setUserId(user);
        log.info("购物车数据:{}", shoppingCart);
        Long dishId = shoppingCart.getDishId();
        //根据当前用户id及本次添加的套餐id/菜品id，查询购物车数据是否存在、
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, user);
        if (dishId != null) {
            //当前添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        } else {
            //当前添加的是套餐
            Long setmealId = shoppingCart.getSetmealId();
            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
        }
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);
        if (shoppingCartServiceOne != null) {
            //如果已经存在原来数量加1
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(shoppingCartServiceOne);
        } else {
            //不存在ze添加到购物车数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartServiceOne = shoppingCart;
        }
        return R.success(shoppingCartServiceOne);
    }

    /**
     * 查询购物车信息
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "查看购物车")
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(HttpServletRequest request) {
        //查询当前登录用户的所有购物车信息
        Long user = (Long) request.getSession().getAttribute("user");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, user);
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 减购物车
     *
     * @param shoppingCart
     * @param request
     * @return
     */
    @ApiOperation(value = "减购物车")
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        //获取套餐id
        Long setmealId = shoppingCart.getSetmealId();
        //获取菜品id
        Long dishId = shoppingCart.getDishId();
        //根据套餐和菜品id获取购物车信息
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealId != null, ShoppingCart::getSetmealId, setmealId);
        queryWrapper.eq(dishId != null, ShoppingCart::getDishId, dishId);
        queryWrapper.eq(BaseContext.getCurrentId() != null, ShoppingCart::getUserId, BaseContext.getCurrentId());
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);
        //当前购物车的唯一id
        Long id = shoppingCartServiceOne.getId();
        //当前购物车的数量
        Integer number = shoppingCartServiceOne.getNumber();
        int i = number - 1;
        if (i > 0) {
            shoppingCartServiceOne.setNumber(i);
            shoppingCartService.updateById(shoppingCartServiceOne);
        } else {
            shoppingCartService.removeById(id);
        }
//        //获取当前用户信息
//        log.info("购物车数据:{}", shoppingCart);
//        Long user = (Long) request.getSession().getAttribute("user");
//        Long currentId = BaseContext.getCurrentId();
//        log.info("当前登录用户id为:{}----{}", user,currentId);
//        shoppingCart.setUserId(user);
//        log.info("购物车数据:{}", shoppingCart);
//        Long dishId = shoppingCart.getDishId();
//        //根据当前用户id及本次添加的套餐id/菜品id，查询购物车数据是否存在、
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId, user);
//        if (dishId != null) {
//            //当前更改的是菜品
//            queryWrapper.eq(ShoppingCart::getDishId, dishId);
//            ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);
//            Integer number;
//            if (shoppingCartServiceOne != null) {
//                number = shoppingCartServiceOne.getNumber() - 1;
//                if (number != null && number > 0) {
//                    shoppingCartServiceOne.setNumber(number);
//                    shoppingCartService.updateById(shoppingCartServiceOne);
//                    return R.success(shoppingCartServiceOne);
//                } else {
//                    LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                    shoppingCartLambdaQueryWrapper.eq(shoppingCartServiceOne.getUserId() != null, ShoppingCart::getUserId, shoppingCartServiceOne.getUserId());
//                    shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
//                }
//            }
//        } else {
//            //当前更改的是套餐
//            Long setmealId = shoppingCart.getSetmealId();
//            queryWrapper.eq(ShoppingCart::getSetmealId, setmealId);
//            ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(queryWrapper);
//            Integer number;
//            if (shoppingCartServiceOne != null) {
//                number = shoppingCartServiceOne.getNumber() - 1;
//                if (number != null && number > 0) {
//                    shoppingCartServiceOne.setNumber(number);
//                    shoppingCartService.updateById(shoppingCartServiceOne);
//                    return R.success(shoppingCartServiceOne);
//                } else {
//                    LambdaQueryWrapper<ShoppingCart> cartLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                    cartLambdaQueryWrapper.eq(shoppingCartServiceOne.getUserId() != null, ShoppingCart::getUserId, shoppingCartServiceOne.getUserId());
//                    shoppingCartService.remove(cartLambdaQueryWrapper);
//                }
//            }
//        }
        return R.success("购物车已更改");
    }

    /**
     * 根据用户信息清空购物车
     *
     * @return
     */
    @ApiOperation(value = "清空购物车")
    @DeleteMapping("/clean")
    public R<String> clean(HttpServletRequest request) {
        Long user = (Long) request.getSession().getAttribute("user");
        Long currentId = BaseContext.getCurrentId();
        log.info("当前登录用户id为:{}----{}", user, currentId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(user != null, ShoppingCart::getUserId, user);
        shoppingCartService.remove(queryWrapper);
        return R.success("购物车清理成功");
    }
}
